using DG.Tweening;
using UnityEngine;

public class IntersectionObject : MonoBehaviour
{
    public bool isSupportTake { get { return m_SupportTake; } }
    public bool isSupportIntersection { get { return m_SupportIntersection; } }
    public bool timePause { get { return m_TimePause; } set { m_TimePause = value; } }
    [SerializeField]
    protected float m_LifeTimeMax = 10;
    protected float m_LifeTime = 10;

    protected float m_IntersectionTimeMax = 5;
    float m_IntersectionProcess = 0;
    protected QuiQuiObj m_IntersectionObj;
    protected QuiQuiObj m_TakingObj;
    ProcessSlider m_ProcessSlider;
    [SerializeField]
    protected bool m_SupportIntersection = true;
    [SerializeField]
    protected bool m_SupportTake = true;

    bool m_TimePause = false;
    private void Awake()
    {
        name = "";
    }

    void Start()
    {
    }

    // Update is called once per frame
    protected virtual void Update()
    {
        if (!m_TakingObj && !m_TimePause)
            m_LifeTime -= Time.deltaTime;
        if (m_IntersectionObj)
        {
            m_IntersectionProcess += Time.deltaTime;
            m_ProcessSlider.SetProcess(m_IntersectionProcess / m_IntersectionTimeMax);
            if (m_IntersectionProcess / m_IntersectionTimeMax > 1)
            {
                OnIntersectionComplated();
            }
        }
        else
        {
        }
    }
    public virtual void OnCreate()
    {
        m_LifeTime = m_LifeTimeMax;
    }
    public virtual bool CanIntersection()
    {
        return true;
    }

    public void OnTake(QuiQuiObj takingQuiqui)
    {
        m_TakingObj = takingQuiqui;
        transform.SetParent(takingQuiqui.transform);
        transform.DOMove(m_TakingObj.transform.position + m_TakingObj.transform.up * m_TakingObj.transform.localScale.x, 0.1f);
    }
    public void OnPlace()
    {
        transform.SetParent(null);
        SetColliderEnabled(false);
        transform.DOMove(m_TakingObj.transform.position + m_TakingObj.transform.up * m_TakingObj.transform.localScale.x, 0.3f);
        DOVirtual.DelayedCall(0.5f, () =>
        {
            SetColliderEnabled(true);
        });
        m_TakingObj = null;
    }
    public void SetColliderEnabled(bool active)
    {
        GetComponent<CircleCollider2D>().enabled = active;
    }
    protected virtual void OnIntersectionComplated()
    {
        if (m_IntersectionObj.GetComponent<PlayerControle>())
            SoundManager.Instance.PlaySound(SoundManager.SoundType.IntersectionComplated, gameObject);
        m_IntersectionObj.intersectingObj = null;
        Clean();
    }
    public virtual void OnIntersection(QuiQuiObj quiqui)
    {
        if (m_IntersectionProcess > 0 || m_SupportIntersection == false)
            return;
        m_IntersectionObj = quiqui;
        m_IntersectionObj.intersectingObj = this;
        m_ProcessSlider = ProcessUIManager.Instance.GetProcessUI();
        m_ProcessSlider.gameObject.SetActive(true);
        m_ProcessSlider.Bind(gameObject);
    }

    protected virtual void OnTriggerEnter2D(Collider2D collision)
    {
    }

    protected virtual void OnTriggerExit2D(Collider2D collision)
    {
        if (m_IntersectionObj && m_IntersectionObj.gameObject == collision.gameObject)
        {
            m_IntersectionObj.intersectingObj = null;
            Clean();
        }
    }

    void Clean()
    {

        m_IntersectionProcess = 0;

        if (m_ProcessSlider)
        {
            ProcessUIManager.Instance.CycleObj(m_ProcessSlider.gameObject);
            m_ProcessSlider = null;
        }
        if (m_IntersectionObj)
        {
            m_IntersectionObj = null;
        }
    }
}
